/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.model.player;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.cmc.music.metadata.IMusicMetadata;
import org.cmc.music.metadata.MusicMetadataSet;
import org.cmc.music.myid3.MyID3;
import org.vintagephone.model.Song;

import android.content.Context;
import android.util.Log;

/**
 * This class is used to scan songs for metadata
 * @author Basil Shikin
 *
 */
public class PlayerMediaScanner 
{
	private static final String Tag = "PlayerMediaScanner";
	
	private static final Executor s_executor = Executors.newSingleThreadExecutor();
	
	private final Context m_context;
	
	public PlayerMediaScanner(Context context) 
	{
		m_context = context;
	}

	public void scanSong( final Song song )
	{
		if ( !song.isScanned() )
		{
			s_executor.execute( new ScanSongTask( song, m_context ) );
		}
	}
	
	private static class ScanSongTask
		implements Runnable
	{
		private final Song m_song;

		ScanSongTask(Song song, Context context) 
		{
			m_song = song;
		}

		public void run() 
		{
			Log.d(Tag, "Scanning " + m_song + "...");
			
			try
			{
				final MusicMetadataSet mp3set = new MyID3().read( m_song.getFile() );
	            final IMusicMetadata mp3data = mp3set.getSimplified();
	            
	            final String track  = mp3data.getSongTitle();
			    final String album  = mp3data.getAlbum();
			    final String artist = mp3data.getArtist();
			    
		        if (track != null && album != null && artist != null) 
		        {
		        	m_song.setSongInformation( track, album, artist );
				    
				    Log.d(Tag, "Scan of " + m_song + " completed: " + track + " - " + artist + " - " + album );	
	            } 
		        else
		        {
		        	Log.w( Tag, "Scan of " + m_song + " failed: song seems to have bad metadata" );		        	
	            }
			}
			catch ( Throwable e )
			{
				Log.e( Tag, "Scan of " + m_song + " failed", e );
			}
		}
	}
}
